#!/usr/bin/env python
from tz.db.pg import PG
from tz.util.day import str2day, day2str
from tz.util.req import requests
from tz.util.extract import extract, extract_li, extract_table


def fetch(com_id, code):
    url = f"http://www.aastocks.com/sc/ipo/companysummary.aspx?view=1&Symbol={code.rjust(5,'0')}"
    print(url)
    html = requests.get(url).content.decode('utf-8', 'ignore')
    html = extract('id="ctl00', '基本公司资料', html).replace('&nbsp;', ' ')
    # day = html.split('</td>')[4].split(">")[-1].strip()
    html = extract_li('<table ', '</table>', html)

    for i in html:
        li = []
        for td in extract_li('<td', '</td>', i):
            txt = td.split(">")[-1].strip()
            if txt:
                li.append(txt)
        if len(li) == 2 and li[0] == '上市日期':
            if li[1] != 'N/A':
                ipo = str2day(li[1])
                if not ipo:
                    return
                print(day2str(ipo))
                PG.insert('stock.com_ipo', dict(
                    day=ipo, id=com_id), on_conflict=None)
    # for tr in li:
    #     for td in tr:
    #         print(td)


def main():
    exist = set(i.id for i in PG.iter('stock.com_ipo'))

    for i in PG.iter('stock.com', where=dict(country='HK')):
        if i.id in exist:
            continue
        fetch(i.id, i.code)

    for i in PG.iter('stock.com_ipo'):
        day = i.day - 1
        count = PG.count_one('stock.com_price', where=dict(
            com_id=i.id,
            day__lt=day
        ))
        if count:
            o = PG.select_one('stock.com', where=dict(id=i.id))
            print(o.name, i.id, i.day, count)
            PG.delete(
                'stock.com_price', where=dict(
                    com_id=i.id, day__lt=day
                )
            )


if __name__ == "__main__":
    main()
    # PG.delete('stock.com_ipo', where=dict(id=0))
